// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

import GlobalData from './GlobalData'

@ccclass
export default class ListItem extends cc.Component {

    @property(cc.SpriteFrame)
    ropFrame: cc.SpriteFrame = null;

    @property()
    price: number = 0;

    @property()
    isAc: boolean = true;
    
    onLoad () {
        
        if(this.ropFrame){
            this.node.getChildByName('rop').getComponent(cc.Sprite).spriteFrame = this.ropFrame;
        }
        if(this.price){
            this.node.getChildByName('price').getComponent(cc.Label).string = `${this.price}`;
        }
        // if(!this.isAc){
        //     this.node.opacity = 200;
        // }
    }

    start () {

    }

    // update (dt) {}

    updateData(){
        const rop = cc.find('Canvas/shop/roplabel').getComponent(cc.Label).string = `套绳: ${GlobalData.ropNum}`
        const money = cc.find('Canvas/shop/money').getComponent(cc.Label).string = `Money: ${GlobalData.money}`
    }

    // 购买套绳
    onBuyRop(){

        if(GlobalData.money < this.price){
            cc.log('aa')
            cc.find('Canvas/tip').getComponent(cc.Animation).play()
            return;
        }

        if(!this.isAc){
            return;
        }

        

        GlobalData.ropNum++;
        GlobalData.money -= this.price;

        this.updateData();
    }
}
